//
//  HCMenuview.m
//  HelloCall
//
//  Created by WangYongpeng on 13-10-15.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCMenuview.h"

@implementation menuDataNode

@synthesize title = m_title;
@synthesize menuId = m_menuId;

- (void)dealloc
{
    self.title = nil;
    
    [super dealloc];
}

@end

@interface HCMenuview()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView* tableView;

@end

@implementation HCMenuview

@synthesize tableView = m_tableView;
@synthesize menuDateArray = m_menuDateArray;
@synthesize delegate = m_delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UIImageView* titleBgView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 20, frame.size.width, 44)] autorelease];
        titleBgView.image = [[UIImage imageNamed:@"CRMListChangeBg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
        [self addSubview:titleBgView];
        
        UIFont* font = [UIFont systemFontOfSize:20];
        NSString* text = @"菜单";
        CGSize size = [text sizeWithFont:font];
        UILabel* titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, (44 - size.height) / 2, frame.size.width, size.height)] autorelease];
        titleLabel.text = text;
        titleLabel.font = font;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [titleBgView addSubview:titleLabel];
        
        //创建列表
        self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 64, frame.size.width, [[UIScreen mainScreen] bounds].size.height - 64)] autorelease];
        [self addSubview:self.tableView];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.tableView];
        
        [self setUserInteractionEnabled:YES];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.menuDateArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    cell = [self.tableView dequeueReusableCellWithIdentifier:@"cellID"];
    
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] init] autorelease];
    }
    
    menuDataNode* node = [self.menuDateArray objectAtIndex:indexPath.row];
    
    cell.textLabel.text = node.title;
    
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    menuDataNode* node = [self.menuDateArray objectAtIndex:indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(menuProcessByMenuID:)])
    {
        [self.delegate menuProcessByMenuID:node.menuId];
    }
}

@end
